package github.kawaiior.yuanchuxiuzhen.client.hud;

import com.mojang.blaze3d.matrix.MatrixStack;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.IGongFa;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.util.MathUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.omg.CORBA.PUBLIC_MEMBER;

@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class HudRenderEvent {

    private static void renderPlayerPanelText(MatrixStack matrixStack, PlayerEntity player, FontRenderer fontRenderer){
        matrixStack.push();

        // 使内容缩放0.7
        float scale = 0.7F;
        matrixStack.scale(scale, scale, scale);

        // 渲染生命值数值
        String healthText = MathUtil.formatFloat(player.getHealth(), 1) + "/" +
                MathUtil.formatFloat(player.getMaxHealth(), 1);
        fontRenderer.drawString(matrixStack, healthText, 51, 8, 0xFFFFFF);

        matrixStack.pop();
    }

    private static void renderManaAndExp(MatrixStack matrixStack, FontRenderer fontRenderer,
                                         String manaText, String expText){
        matrixStack.push();

        // 使内容缩放0.5
        float scale = 0.5F;
        matrixStack.scale(scale, scale, scale);

        // 渲染Mana和经验值
        fontRenderer.drawString(matrixStack, manaText, 72, 35, 0xFFFFFF);
        fontRenderer.drawString(matrixStack, expText, 72, 49, 0xFFFFFF);

        matrixStack.pop();
    }

    private static void renderPlayerGongFa(MatrixStack matrixStack, FontRenderer fontRenderer, int exp, int maxExp){
        AbstractGui.fill(matrixStack, 2, 30, 136, 36, 0xFF040507);
        AbstractGui.fill(matrixStack, 3, 31, 3+(int)(132F*exp/maxExp), 35, 0xFF00B4FF);
        matrixStack.push();
        // 使内容缩放0.5
        float scale = 0.5F;
        matrixStack.scale(scale, scale, scale);
        fontRenderer.drawString(matrixStack, exp+"/"+maxExp, 100, 62, 0xFFFFFF);
        matrixStack.pop();
    }

    private static void renderPlayerPanel(RenderGameOverlayEvent event){
        MatrixStack matrixStack = event.getMatrixStack();
        matrixStack.push();

        // 渲染属性条
        Minecraft mc = Minecraft.getInstance();
        FontRenderer fontRenderer = mc.fontRenderer;

        ClientPlayerEntity player = mc.player;

        // 生命值
        AbstractGui.fill(matrixStack, 34, 2, 136, 15, 0xFF040507);
        AbstractGui.fill(matrixStack, 35, 3, 135, 14, 0xFF15202B);
        AbstractGui.fill(matrixStack, 35, 3, 35+(int)(100F*player.getHealth()/player.getMaxHealth()),
                14, 0xFFFF0000);

        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            // 等级文本
            TranslationTextComponent levelText = new TranslationTextComponent("ycxz.player.level."+power.getLevel());
            AbstractGui.fill(matrixStack, 2, 2, 33, 15, 0xFF040507);
            AbstractGui.fill(matrixStack, 3, 3, 32, 14, 0xFF15202B);
            fontRenderer.drawString(matrixStack, levelText.getString(), 9, 4, 0xFFFFFF);

            TranslationTextComponent innerLevelText;
            if (power.isMaxExperience()){
                innerLevelText = new TranslationTextComponent("ycxz.player.level.inner.max");
            }else if (power.getLevel()==0){
                innerLevelText = new TranslationTextComponent("ycxz.player.level.inner.0");
            }else {
                innerLevelText = new TranslationTextComponent("ycxz.player.level.inner."+power.getInnerLevel());
            }

            AbstractGui.fill(matrixStack, 2, 16, 33, 29, 0xFF040507);
            AbstractGui.fill(matrixStack, 3, 17, 32, 28, 0xFF15202B);
            fontRenderer.drawString(matrixStack, innerLevelText.getString(), 9, 18, 0xFFFFFF);

            // 灵力
            float manaLen = Math.max(power.getMana()/power.getMaxMana(), 0F);
            AbstractGui.fill(matrixStack, 34, 16, 136, 22, 0xFF040507);
            AbstractGui.fill(matrixStack, 35, 17, 135, 21, 0xFF15202B);
            AbstractGui.fill(matrixStack, 35, 17, 35+(int)(100F*manaLen),
                    21, 0xFF00B4FF);

            // 经验
            float expLen = Math.max((power.getExperience()+0.001F)/power.getMaxExperience(), 0F);
            AbstractGui.fill(matrixStack, 34, 23, 136, 29, 0xFF040507);
            AbstractGui.fill(matrixStack, 35, 24, 135, 28, 0xFF15202B);
            AbstractGui.fill(matrixStack, 35, 24, 35+(int)(100F*expLen),
                    28, 0xFFD4CA57);

            String manaText = MathUtil.formatFloat(power.getMana(), 1) + "/" +
                    MathUtil.formatFloat(power.getMaxMana(), 1);
            String expText = power.getExperience() + "/" + power.getMaxExperience();
            renderManaAndExp(matrixStack, fontRenderer, manaText, expText);

            // 功法
            IGongFa gongFa = power.getLearningGongFaObj();
            if (gongFa!=null){
                renderPlayerGongFa(matrixStack, fontRenderer, power.getGongFaExp(), gongFa.getMaxExperience());
            }
        });

        renderPlayerPanelText(matrixStack, player, fontRenderer);

        matrixStack.pop();
    }

    @SubscribeEvent
    public static void onOverlayRender(RenderGameOverlayEvent event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.ALL){
            renderPlayerPanel(event);
            return;
        }
        if (event.getType() != RenderGameOverlayEvent.ElementType.HEALTH) {
            return;
        }
        event.setCanceled(true);
    }

}
